# from main import app
from pydantic import BaseModel
from typing import Optional
from fastapi import Header, UploadFile, APIRouter
import base64
from utils.jwt.gettoken import get_token, verify_token
# 用户相关
from src.app.authorization.authorization import authorization, get_openid
from src.app.user.users import userinfo, update_userinfo, is_exist
# 工头相关
from src.app.ganger.ganger import ganger_list, add_ganger, update_ganger, remove_ganger, current_ganger, ganger_one
# 预支工资相关
from src.app.advance.advance import add_advance, advance_list
# 工天记录相关
from src.app.record.record import calculate, record, record_lists, one_record, update_record
# 统计相关
from src.app.statistics.statistics import statistics_data_all
# 反馈
from utils.sql.app.feedback import feedbacks
# 消息
from utils.sql.public.message import messages

app_router = APIRouter()
invalid_token = {
    "code": 205,
    "data": '',
    "message": "token have expired"
}
"""
微信小程序授权
:param code 获取openID的code
:author scw 2024年3月5日
"""


class Auth(BaseModel):
    code: str
    headimg: str
    nickname: str
    share_id: str


@app_router.post('/api/manday/app/authorization')
async def wx_authorization(data: Auth):
    return await authorization(data)


# 上传文件
@app_router.post('/api/manday/app/uploadFile')
async def up_files(file: UploadFile):
    # print(file.headers['content-type'])
    type = file.headers['content-type']
    contents = await file.read()
    encoded_file = base64.b64encode(contents).decode('utf-8')
    return {
        "code": 200,
        "data": f"data:{type};base64,{encoded_file}",
        "meaagse": "上传成功"
    }


"""
刷新token
:author scw 2024年3月5日
"""


@app_router.get('/api/manday/app/getToken/{user_id}')
async def get_tokens(user_id: str):
    return await get_token(user_id)


"""
获取个人信息
:author scw 2024年3月5日
"""


@app_router.get('/api/manday/app/userInfo')
async def get_userinfo(Authorization: Optional[str] = Header(None)):
    user = await verify_token(Authorization)
    if user:
        return await userinfo(user['user_id'])
    else:
        return invalid_token


"""
修改个人信息
:author scw 2024年3月5日
"""


class Info(BaseModel):
    user_name: str
    phone_num: str
    day_manhour: str
    daily_wage: float
    hourly_wage: float
    job_location: str


@app_router.post('/api/manday/app/userInfo')
async def set_userinfo(data: Info, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await update_userinfo(data, token['user_id'])
    else:
        return invalid_token


"""
单独获取openID
:author scw 2024年3月5日
"""


@app_router.get('/api/manday/app/openID/{code}')
async def openid(code: str):
    return await get_openid(code)


"""
查询数据库是否存在用户
:author scw 2024年3月5日
"""


@app_router.get('/api/manday/app/isUser/{userid}')
async def exist_user(userid: str):
    return await is_exist(userid)


"""
获取工头列表
:author scw 2024年3月5日
"""


@app_router.get('/api/manday/app/gangerlist')
async def ganger_all(current: int, query: str = '', Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await ganger_list(query, current, token['user_id'])
    else:
        return invalid_token


"""
获取单个工头信息
:author scw 2024年3月5日
"""


@app_router.get('/api/manday/app/gangerone/{ganger_id}')
async def gangerone(ganger_id, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await ganger_one(ganger_id, token['user_id'])
    else:
        return invalid_token


"""
添加工头
:author scw 2024年3月5日
"""


class Ganger(BaseModel):
    ganger_name: str
    ganger_phone: str


@app_router.post('/api/manday/app/addganger')
async def addition_ganger(data: Ganger, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await add_ganger(data, token['user_id'])
    else:
        return invalid_token


"""
更新工头信息
:author scw 2024年3月5日
"""


class Gangers(BaseModel):
    id: int
    user_id: str
    ganger_name: str
    ganger_phone: str


@app_router.post('/api/manday/app/updateganger')
async def update_Gangers(data: Gangers, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await update_ganger(data, token['user_id'])
    else:
        return invalid_token


"""
删除工头
:author scw 2024年3月5日
"""


@app_router.get('/api/manday/app/deleteganger/{id}')
async def delete_gangers(id: int, dedaults: str, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await remove_ganger(id, dedaults, token['user_id'])
    else:
        return invalid_token


"""
设置工头为当前
:author scw 2024年3月5日
"""


@app_router.get('/api/manday/app/currentganger/{ganger_id}')
async def set_gangers(ganger_id, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await current_ganger(ganger_id, token['user_id'])
    else:
        return invalid_token


"""
添加预支金额
:author scw 2024年3月5日
"""


class Advance(BaseModel):
    advance_date: str
    advance_money: str


@app_router.post('/api/manday/app/addadvance')
async def advance(advance: Advance, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await add_advance(advance, token['user_id'])
    else:
        return invalid_token


"""
查询预支金额
:author scw 2024年3月5日
"""


@app_router.get('/api/manday/app/advanceList')
async def advanceList(current: int, query: str, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await advance_list(current, query, token['user_id'])
    else:
        return invalid_token


"""
填写工天时动态计算工时工资
:author scw 2024年3月5日
"""


@app_router.get('/api/manday/app/calculate')
async def calculate_money(job_date, start_time, end_time, half_time, over_time,
                          Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await calculate(job_date, start_time, end_time, half_time, over_time, token['user_id'])
    else:
        return invalid_token


"""
提交工天记录
:author scw 2024年3月5日
"""


# job_date, start_time, end_time, half_time, over_time
class Record(BaseModel):
    job_date: str
    start_time: str
    end_time: str
    half_time: str
    over_time: str


@app_router.post('/api/manday/app/addrecord')
async def apend_record(data: Record, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await record(data, token['user_id'])
    else:
        return invalid_token


# 查询工天列表
@app_router.get('/api/manday/app/recordList')
async def record_list(year: str, month: str, current: int, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await record_lists(year, month, current, token['user_id'])
    else:
        return invalid_token


#

# 查询单个工天
@app_router.get('/api/manday/app/recordone/{id}')
async def record_ones(id: str, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await one_record(id, token['user_id'])
    else:
        return invalid_token


class Records(BaseModel):
    id: int
    job_date: str
    start_time: str
    end_time: str
    half_time: str
    over_time: str


# 修改单个工天
@app_router.post('/api/manday/app/updaterecordone')
async def update_record_ones(data: Records, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await update_record(data, token['user_id'])
    else:
        return invalid_token


# 工天数据统计
@app_router.get('/api/manday/app/statistics')
async def statistics(isMonth: bool, year: str, month: str, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return await statistics_data_all(isMonth, year, month, token['user_id'])
    else:
        return invalid_token


# 反馈
class Feedbacks(BaseModel):
    content: str
    phone_num: str


@app_router.post('/api/manday/app/feedback')
async def post_feedback(data: Feedbacks, Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return feedbacks(data, token['user_id'])
    else:
        return invalid_token


# 获取消息
@app_router.get('/api/manday/app/messages')
async def get_message(Authorization: Optional[str] = Header(None)):
    token = await verify_token(Authorization)
    if token:
        return messages(token['user_id'])
    else:
        return invalid_token


